package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service // 注册成Bean对象
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        //1.设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //2.调用Mapper层进行查询, 并获取数据,强转成Page对象 -- > 需要菜品名称使用VO封装数据
        Page<SetmealVO> page = setmealMapper.list(dto);
        //3.封装PageResult对象并返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param dto
     */
    @Transactional // 0.涉及setmeal_dish与setmeal表,开事务
    @Override
    public void addSetmeal(SetmealDTO dto) {
        //1.构造Setmeal对象, 调用mapper层,将数据存入到swtmeal表中
        //1.1完成属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //1.2调用
        setmealMapper.insert(setmeal);
        //2.获取套餐关联包含的菜品
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //2.1缺少套餐id, 需要补上
        //判断
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        //2.2调用mapper层方法,将数据更新到setmeal_dish表
        setmealDishMapper.insert(setmealDishes);
    }


    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        //1.调用mapper层, 从 setmeal表中获取数据
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        //2.查询setmeal_dish表, 获取数据
        List<SetmealDish> select = setmealDishMapper.selectbySermealId(id);
        //3.封装结果,并返回
        //3.1创建对象并拷贝
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(select);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param dto
     */
    @Override
    @Transactional //0.涉及多张表, 开事务
    public void updateSetmeal(SetmealDTO dto) {
        //1.构造Setmeal对象, 跟新setmeal表中的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmealMapper.update(setmeal);
        //2.根据setmealId删除, setmeal_dish中的所有数据
        setmealDishMapper.delete(dto.getId());
        //3.获取套餐菜品数据, 做非空判断,并补齐字段
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
        }
        //4.进行插入到setmeal_dish表中
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 套餐起售与停售
     *
     * @param status
     * @param id
     */
    @Override
    public void enableOrDishable(Integer status, Long id) {
        //3.起售套餐是, 判断套餐是否有停售菜品, 有停售菜品提示"套餐内包含未起售菜品, 无法起售"
        //未起售的套餐才需要如此
        if (status == StatusConstant.ENABLE) {
            //select a.* from dish left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && !dishList.isEmpty()) {
                dishList.forEach(dish -> {
                    if(dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }
        //1.构造setmeal对象
        Setmeal setmeal = Setmeal
                .builder()
                .id(id)
                .status(status)
                .build();
        //2.调用mapper层进行更新状态
        setmealMapper.update(setmeal);
    }

    @Override
    @Transactional // 0. 涉及多张表, 开启事务
    public void delete(List<Long> ids) {
        //1.判断套餐是否在起售状态, 起售不允许删除
        if(ids != null && !ids.isEmpty()) {
            ids.forEach(id -> {
                Setmeal setmeal = setmealMapper.selectSetmealById(id);
                if(setmeal.getStatus() == StatusConstant.ENABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            });
        }
        //2.删除setmeal_dish表中的套餐关联菜品
        if(ids != null && !ids.isEmpty()) {
            ids.forEach(id -> {
                setmealDishMapper.delete(id);
            });
            //3.删除setmeal表中的套餐
            setmealMapper.deleteBatch(ids);
        }

    }


    /**
     * 用户端-条件查询  直接导入的
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.listByCategoryId(setmeal);
        return list;
    }

    /**
     * 用户端-根据id查询菜品选项  直接导入的
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
